home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 September (Japanese) / CICA Shareware for Windows CD-ROM (Walnut Creek) (September 1995) (Japanese) (Disc 2).iso / disc2 / nt / ntperf.exe / PERFTOOLS / SRC / PERFMON / SYSTEM.C_ / SYSTEM.C
Encoding:
C/C++ Source or Header  |  1993-05-15  |  12.6 KB  |  431 lines

  1. //
  2. //  Foreign computer support needs more work (a-robw)
  3. //
  4. #ifdef FOREIGN_COMPUTER_SUPPORT
  5. #undef FOREIGN_COMPUTER_SUPPORT
  6. #endif
  7.  
  8. #include "perfmon.h"
  9. #include "system.h"     // external declarations for this file
  10.  
  11. #include "perfdata.h"
  12. #include "perfmops.h"
  13. #include "playback.h"   // for PlayingBackLog
  14. #include "pmemory.h"
  15. #include "utils.h"      // for strsame, et al
  16. #include "sizes.h"
  17.  
  18. DWORD
  19. SystemCount(
  20.     PPERFSYSTEM pSystemFirst
  21. )
  22. {
  23.     PPERFSYSTEM       pSystem ;
  24.     DWORD           iNumSystems ;
  25.  
  26.     iNumSystems = 0 ;
  27.  
  28.     for (pSystem = pSystemFirst ;
  29.          pSystem ;
  30.          pSystem = pSystem->pSystemNext) {
  31.         iNumSystems++ ;
  32.     }
  33.  
  34.     return iNumSystems ;
  35. }
  36.  
  37. PPERFSYSTEM
  38. SystemCreate (
  39.     LPCTSTR lpszSystemName
  40. )
  41. {
  42.     PPERFSYSTEM     pSystem ;
  43.     PPERFDATA       pLocalPerfData;
  44.     DWORD           Status ;
  45.     DWORD           dwMemSize;
  46.     TCHAR           GlobalValueBuffer[] = L"Global" ;
  47.     TCHAR           ForeignValueBuffer[8+MAX_COMPUTERNAME_LENGTH+1] =
  48.                     L"Foreign " ;
  49.  
  50.     // attempt to allocate system data structure
  51.  
  52.     pSystem = MemoryAllocate (sizeof (PERFSYSTEM)) ;
  53.     if (!pSystem) {
  54.         SetLastError (ERROR_OUTOFMEMORY);
  55.         return (NULL) ;
  56.     }
  57.  
  58.     // initialize name and help table pointers
  59.  
  60.     pSystem->CounterInfo.pNextTable = NULL;
  61.     pSystem->CounterInfo.dwLangId = 0;
  62.     pSystem->CounterInfo.dwLastId = 0;
  63.     pSystem->CounterInfo.TextString = NULL;
  64.  
  65.     lstrcpy (pSystem->sysName, lpszSystemName) ;
  66.    
  67.     // try to open key to registry, error code is in GetLastError()
  68.  
  69.     pSystem->sysDataKey = OpenSystemPerfData(lpszSystemName);
  70.  
  71.     // if a Null Key was returned then:
  72.     //  a) there's no such computer
  73.     //  b) the system is a foreign computer
  74.     //
  75.     //  before giving up, then see if it's a foreign computer
  76.  
  77.     if (!pSystem->sysDataKey) {
  78.         
  79.         // build foreign computer string
  80.  
  81.         lstrcat(ForeignValueBuffer, lpszSystemName) ;
  82.         
  83.         // assign System value name pointer to the local variable for trial
  84.  
  85.         pSystem->lpszValue = ForeignValueBuffer;
  86.  
  87.         // try to get data from the computer to see if it's for real
  88.         // otherwise, give up and return NULL
  89.  
  90.         pLocalPerfData = MemoryAllocate (STARTING_SYSINFO_SIZE);
  91.         if (pLocalPerfData == NULL) { // no mem so give up
  92.             SetLastError (ERROR_OUTOFMEMORY);
  93.             SystemFree (pSystem, TRUE);
  94.             return (NULL);
  95.         } else {
  96.             pSystem->sysDataKey = HKEY_PERFORMANCE_DATA; // local machine
  97.  
  98.             dwMemSize = STARTING_SYSINFO_SIZE;
  99.             Status = GetSystemPerfData (
  100.                     pSystem->sysDataKey,
  101.                     pSystem->lpszValue,
  102.                     pLocalPerfData,
  103.                     &dwMemSize);
  104.  
  105.             // success means a valid buffer came back
  106.             // more data means someone tried (so it's probably good (?)
  107.  
  108.             if (!((Status == ERROR_MORE_DATA) || (Status == ERROR_SUCCESS)) ||
  109.                 !((pLocalPerfData->Signature[0] == (WCHAR)'P') &&
  110.                   (pLocalPerfData->Signature[1] == (WCHAR)'E') &&
  111.                   (pLocalPerfData->Signature[2] == (WCHAR)'R') &&
  112.                   (pLocalPerfData->Signature[3] == (WCHAR)'F'))) {
  113.                 SetLastError (ERROR_BAD_NET_NAME); // unable to find name
  114.                 SystemFree (pSystem, TRUE);
  115.                 MemoryFree (pLocalPerfData);    // don't really need anything from it
  116.                 return NULL;
  117.             }
  118.  
  119.             MemoryFree (pLocalPerfData);    // don't really need anything from it
  120.     
  121.             // ok, so we've established that a foreign data provider
  122.             // exists, now to finish the initialization.
  123.  
  124.             // change system name in structure to get counter names
  125.  
  126.             lstrcpy (pSystem->sysName, LocalComputerName);
  127.  
  128.             Status = GetSystemNames(pSystem);   // get counter names & explain text
  129.             if (Status != ERROR_SUCCESS) {
  130.                 // unable to get names so bail out
  131.                 SetLastError (Status);
  132.                 SystemFree (pSystem, TRUE);
  133.                 return (NULL) ;
  134.             }
  135.  
  136.             // restore computer name for displays, etc.
  137.  
  138.             lstrcpy (pSystem->sysName, lpszSystemName);
  139.     
  140.             // allocate value string buffer
  141.             pSystem->lpszValue = MemoryAllocate (TEMP_BUF_LEN*sizeof(WCHAR));
  142.             if (!pSystem->lpszValue) {
  143.                 // unable to allocate memory
  144.                 SetLastError (ERROR_OUTOFMEMORY);
  145.                 SystemFree (pSystem, TRUE);
  146.                 return (NULL) ;
  147.             } else {
  148.                 lstrcpy (pSystem->lpszValue, ForeignValueBuffer);
  149.             }
  150.         }
  151.     } else {
  152.         // if here, then a connection to the system's registry was established
  153.         // so continue with the system data structure initialization
  154.  
  155.         // get counter names & explain text from local computer
  156.  
  157.         Status = GetSystemNames(pSystem);   
  158.         if (Status != ERROR_SUCCESS) {
  159.             // unable to get names so bail out
  160.             SetLastError (Status);
  161.             SystemFree (pSystem, TRUE);
  162.             return (NULL) ;
  163.         }
  164.  
  165.         // allocate value string buffer
  166.         pSystem->lpszValue = MemoryAllocate(TEMP_BUF_LEN*sizeof(WCHAR));
  167.  
  168.         if (!pSystem->lpszValue) {
  169.             // unable to allocate memory
  170.             SetLastError (ERROR_OUTOFMEMORY);
  171.             SystemFree (pSystem, TRUE);
  172.             return (NULL) ;
  173.         } else {
  174.             SetSystemValueNameToGlobal (pSystem);
  175.         }
  176.     }
  177.  
  178.     // initialize remaining system pointers
  179.  
  180.     pSystem->pSystemNext = NULL ;
  181.     pSystem->FailureTime = 0;
  182.  
  183.     SetLastError (ERROR_SUCCESS);
  184.  
  185.     return (pSystem) ;
  186. }  // SystemCreate
  187.  
  188. PPERFSYSTEM
  189. SystemGet (
  190.     PPERFSYSTEM pSystemFirst,
  191.     LPCTSTR lpszSystemName
  192. )
  193. {
  194.     PPERFSYSTEM       pSystem ;
  195.  
  196.     if (!pSystemFirst) {
  197.         return (NULL) ;
  198.     }
  199.     
  200.     for (pSystem = pSystemFirst ;
  201.          pSystem ;
  202.          pSystem = pSystem->pSystemNext) {
  203.         if (strsamei (pSystem->sysName, lpszSystemName)) {
  204.             return (pSystem) ;
  205.         }
  206.     }  // for
  207.  
  208.     return (NULL) ;
  209. }
  210.  
  211. PPERFSYSTEM
  212. SystemAdd (
  213.     PPPERFSYSTEM ppSystemFirst,
  214.     LPCTSTR lpszSystemName
  215. )
  216. {
  217.     PPERFSYSTEM       pSystem ;
  218.     PPERFSYSTEM       pSystemPrev ;
  219.  
  220.  
  221.     if (!*ppSystemFirst) {
  222.         *ppSystemFirst = SystemCreate (lpszSystemName) ;
  223.         return (*ppSystemFirst) ;
  224.     }
  225.  
  226.     for (pSystem = *ppSystemFirst ;
  227.          pSystem ;
  228.          pSystem = pSystem->pSystemNext) {
  229.         pSystemPrev = pSystem ;
  230.         if (strsamei (pSystem->sysName, lpszSystemName)) {
  231.             return (pSystem) ;
  232.         }
  233.     }  // for
  234.  
  235.     pSystemPrev->pSystemNext = SystemCreate (lpszSystemName) ;
  236.     return (pSystemPrev->pSystemNext) ;
  237. }
  238.  
  239. void
  240. SystemFree (
  241.     PPERFSYSTEM pSystem,
  242.     BOOL        bDeleteTheSystem
  243. )
  244. {  // SystemFree
  245.  
  246.     PCOUNTERTEXT pCounter, pNextCounter;
  247.  
  248.     if (!pSystem) {
  249.         // can't proceed
  250.         return ;
  251.     }
  252.  
  253.     if (pSystem->sysDataKey && pSystem->sysDataKey != HKEY_PERFORMANCE_DATA) {
  254.         // close the remote computer key
  255.         RegCloseKey (pSystem->sysDataKey);
  256.         pSystem->sysDataKey = 0 ;
  257.     }
  258.  
  259.     for (pCounter = pSystem->CounterInfo.pNextTable, pNextCounter = NULL;
  260.          pCounter;
  261.          pCounter = pNextCounter) {
  262.         pNextCounter = pCounter->pNextTable;
  263.         MemoryFree (pCounter);
  264.     }
  265.     pSystem->CounterInfo.pNextTable = NULL ;
  266.  
  267.     if (pSystem->CounterInfo.TextString) {
  268.         MemoryFree (pSystem->CounterInfo.TextString);
  269.         pSystem->CounterInfo.TextString = NULL ;
  270.     }
  271.  
  272.     if (pSystem->CounterInfo.HelpTextString) {
  273.         MemoryFree (pSystem->CounterInfo.HelpTextString);
  274.         pSystem->CounterInfo.HelpTextString = NULL ;
  275.     }
  276.     pSystem->CounterInfo.dwLastId = 0 ;
  277.     pSystem->CounterInfo.dwHelpSize = 0 ;
  278.     pSystem->CounterInfo.dwCounterSize = 0 ;
  279.  
  280.     if (bDeleteTheSystem) {
  281.         if (pSystem->lpszValue) {
  282.             MemoryFree (pSystem->lpszValue);
  283.             pSystem->lpszValue = NULL ;
  284.         }
  285.         MemoryFree (pSystem) ;
  286.     }
  287. }
  288.  
  289. void
  290. DeleteUnusedSystems (
  291.     PPPERFSYSTEM  ppSystemFirst ,
  292.     int           iNoUseSystems
  293. )
  294. {
  295.     PPERFSYSTEM   pPrevSys, pCurrentSys, pNextSys ;
  296.  
  297.     // delete all the marked system from the list header until
  298.     // we hit one that is not marked
  299.     while ((*ppSystemFirst)->bSystemNoLongerNeeded) {
  300.        // delect from the list header
  301.        pCurrentSys = *ppSystemFirst ;
  302.        *ppSystemFirst = pCurrentSys->pSystemNext ;
  303.        SystemFree (pCurrentSys, TRUE) ;
  304.        iNoUseSystems-- ;
  305.        if (iNoUseSystems <= 0 || !(*ppSystemFirst)) {
  306.           // done
  307.           break ;
  308.        }
  309.     }
  310.  
  311.     if (iNoUseSystems <= 0 || !(*ppSystemFirst)) {
  312.        return ;
  313.     }
  314.  
  315.     // now walk the list and delete each marked system
  316.     for (pPrevSys = *ppSystemFirst, pCurrentSys = pPrevSys->pSystemNext ;
  317.          pCurrentSys && iNoUseSystems > 0 ;
  318.          pCurrentSys = pNextSys, iNoUseSystems--) {
  319.  
  320.        if (pCurrentSys->bSystemNoLongerNeeded) {
  321.           // the current system is marked, updated the list and free
  322.           // this system.  No need to change pPrevSys here
  323.           pNextSys = pPrevSys->pSystemNext = pCurrentSys->pSystemNext ;
  324.           SystemFree (pCurrentSys, TRUE) ;
  325.        } else {
  326.           // pCurrentSys is OK, update the 2 list pointers and 
  327.           // carry on looping
  328.           pPrevSys = pCurrentSys ;
  329.           pNextSys = pCurrentSys->pSystemNext ;
  330.        }
  331.     }
  332. }
  333.  
  334. void
  335. FreeSystems (
  336.     PPERFSYSTEM pSystemFirst
  337. )
  338. {
  339.     PPERFSYSTEM    pSystem, pSystemNext ;
  340.  
  341.  
  342.     for (pSystem = pSystemFirst; 
  343.          pSystem; 
  344.          pSystem = pSystemNext) {
  345.         pSystemNext = pSystem->pSystemNext ;
  346.         SystemFree (pSystem, TRUE) ;
  347.     }
  348. }  // FreeSystems
  349.  
  350. PPERFSYSTEM
  351. GetComputer (
  352.     HDLG hDlg,
  353.     WORD wControlID,
  354.     BOOL bWarn,
  355.     PPERFDATA *ppPerfData,
  356.     PPERFSYSTEM *ppSystemFirst
  357. )
  358. /*
  359.    Effect:        Attempt to set the current computer to the one in the
  360.                   hWndComputers dialog edit box. If this computer system
  361.                   can be found, load the objects, etc. for the computer
  362.                   and set pSystem and ppPerfdata to the values for this
  363.                   system.
  364. */
  365. {  // GetComputer
  366.     TCHAR          szComputer [MAX_SYSTEM_NAME_LENGTH + 1] ;
  367.     PPERFSYSTEM    pSystem;
  368.     TCHAR          tempBuffer [LongTextLen] ;
  369.     DWORD          dwBufferSize ;
  370.     LPTSTR         pBuffer = NULL ;
  371.  
  372.     DialogText (hDlg, wControlID, szComputer) ;
  373.  
  374.     // If necessary, add the system to the lists for this view.
  375.     pSystem = SystemGet (*ppSystemFirst, szComputer) ;
  376.     if (!pSystem) {
  377.         pSystem = SystemAdd (ppSystemFirst, szComputer) ;
  378.     }
  379.  
  380.     if (!pSystem && bWarn) {
  381.         DialogSetString (hDlg, wControlID, LocalComputerName) ;
  382.  
  383.         // Note: this will succeed since the local computer is always
  384.         // available
  385.         EditSetModified (GetDlgItem(hDlg, wControlID), FALSE) ;
  386.     
  387.         pSystem = SystemGet (*ppSystemFirst, LocalComputerName) ;
  388.         if (!pSystem) {
  389.             pSystem = SystemAdd (ppSystemFirst, LocalComputerName) ;
  390.         }
  391.  
  392. //        MessageBoxResource (hDlg, IDS_COMPUTERNOTFOUND, IDS_APPNAME, MB_OK) ;
  393.         DlgErrorBox (hDlg, ERR_COMPUTERNOTFOUND) ;
  394.  
  395.         SetFocus (DialogControl(hDlg, wControlID)) ;
  396.     }
  397.  
  398.     if (pSystem) {
  399.         if (PlayingBackLog ()) {
  400.             *ppPerfData =
  401.             LogDataFromPosition (pSystem, &(PlaybackLog.StartIndexPos)) ;
  402.         } else {
  403.  
  404.             if (pSystem->lpszValue) {
  405.                // save the previous lpszValue string before 
  406.                // SetSystemValueNameToGlobal screw it up
  407.                dwBufferSize = MemorySize (pSystem->lpszValue) ;
  408.                if (dwBufferSize <= sizeof(tempBuffer)) {
  409.                   pBuffer = tempBuffer ;
  410.                } else {
  411.                   pBuffer = MemoryAllocate (dwBufferSize) ;
  412.                }
  413.                memcpy (pBuffer, pSystem->lpszValue, dwBufferSize) ;
  414.             }
  415.  
  416.             SetSystemValueNameToGlobal (pSystem);
  417.             UpdateSystemData (pSystem, ppPerfData) ;
  418.  
  419.             if (pSystem->lpszValue) {
  420.                // retore the previous lpszValue string
  421.                memcpy (pSystem->lpszValue, pBuffer, dwBufferSize) ;
  422.                if (pBuffer != tempBuffer) {
  423.                   MemoryFree (pBuffer) ;
  424.                }
  425.             }
  426.         }
  427.     }
  428.     return (pSystem) ;
  429.  
  430. }  // GetComputer
  431.